import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_colors.dart';
import 'package:mall/constant/app_dimens.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/gen/r.dart';
import 'package:mall/modules/business.dart';
import 'package:mall/modules/model/account.dart';
import 'package:mall/modules/service/user_service.dart';
import 'package:mall/router/route_names.dart';
import 'package:mall/ui/page/login/widgets/agreement_widget.dart';
import 'package:mall/ui/page/login/widgets/input_widget.dart';
import 'package:mall/ui/widgets/loading_dialog.dart';
import 'package:mall/utils/ext/widget_ext.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mall/utils/hud/hud_util.dart';
import 'package:mall/utils/keyboard/keyboard_util.dart';

class LoginWithPassword extends StatefulWidget {
  @override
  State<LoginWithPassword> createState() => _LoginWithPasswordState();
}

class _LoginWithPasswordState extends State<LoginWithPassword> {
  final TextEditingController _accountController = TextEditingController();

  final TextEditingController _passWordController = TextEditingController();

  //是否阅读并同意了协议
  RxBool hasReadProtocol = false.obs;

  @override
  void initState() {
    //获取账号信息
    UserService.getAccount().then((value) {
      if (value != null) {
        _accountController.text = value.account ?? '';
        _passWordController.text = value.passWord ?? '';
      }
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        actions: [
          TextButton(
              onPressed: () {
                Get.offAndToNamed(MobileRouteNames.login_phone);
              },
              child: Text(
                '验证码登录/注册',
                style: APPTextStyle.color_666666_size_13,
              )),
        ],
      ),
      body: SingleChildScrollView(
        child: Container(
          // alignment: Alignment.centerLeft,
          margin: EdgeInsets.only(left: 33.w, right: 33.w, top: 50),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '账号密码登陆',
                style: TextStyle(
                  color: AppColors.C_222222,
                  fontSize: 25,
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(height: 53),
              //输入账号
              _phoneTextField(),
              SizedBox(height: 20),
              //输入验证码
              _code(),
              SizedBox(height: 33),
              //登录
              _loginBtn().onInkwellTap(() => _login(context)),
              SizedBox(height: 18),
              //隐私协议
              AgreementWidget(hasReadProtocol: hasReadProtocol),
            ],
          ),
        ),
      ),
    ).closeKeyboard(context);
  }

  Container _loginBtn() {
    return Container(
      alignment: Alignment.center,
      width: double.infinity,
      height: 50,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(25)),
        color: AppColors.C_FF5722,
      ),
      child: Text('登录/注册', style: TextStyle(color: Colors.white, fontSize: 16)),
    );
  }

  Widget _code() {
    return InputWidget(
      leftIcon: R.image.labePhone.path,
      textField: CupertinoTextField(
        // keyboardType: TextInputType.phone,
        obscureText: true,
        placeholder: '请输入密码',
        clearButtonMode: OverlayVisibilityMode.editing,
        controller: _passWordController,
        decoration: BoxDecoration(
          border: null,
          color: Colors.transparent,
        ),
      ),
    );
  }

  Widget _phoneTextField() {
    return InputWidget(
      leftIcon: R.image.labePhone.path,
      textField: CupertinoTextField(
        // keyboardType: TextInputType.phone,
        placeholder: '请输入用户名/手机号',
        maxLength: 11,
        clearButtonMode: OverlayVisibilityMode.editing,
        controller: _accountController,
        decoration: BoxDecoration(
          border: null,
          color: Colors.transparent,
        ),
      ),
    );
  }

  //去登录
  void _login(BuildContext context) {
    //判断手机号是否存在
    if (_accountController.text.isEmpty) {
      HUDUtil.toast('请输入用户名/手机号');
      return;
    }
    //判断验证码是否存在
    if (_passWordController.text.isEmpty) {
      HUDUtil.toast('请输入密码');
      return;
    }
    //判断是否同意了协议
    if (!hasReadProtocol.value) {
      HUDUtil.toast('请阅读并同意协议');
      return;
    }

    HUDUtil.show('登录中...');

    Business.login(_accountController.text, _passWordController.text).then((response) {
      HUDUtil.toast('登录成功');
      Get.back();
    }).catchError((e) {
      HUDUtil.dismiss();
    });
  }
}
